package crud;

import javax.persistence.EntityManager;

import entities.ExamineDetail;
import entities.LabServiceDetail;
import entities.LabServiceInfo;

public class LabServiceDetailCRUD extends AbstractGRUD<LabServiceDetail> {
	public LabServiceDetailCRUD() {
		super(LabServiceDetail.class);
	}

	public String create(int idLabServiceInfo, String result, int idExamine) {
		EntityManager em = this.getEm();
		String rslt = "success";
		try {
			em.getTransaction().begin();
			LabServiceInfo labServiceInfo = new LabServiceInfoCRUD().readById(idLabServiceInfo);
			ExamineDetail examineDetail = new ExamineDetailCRUD().readById(idExamine);
			LabServiceDetail labSeviceDetail = new LabServiceDetail(labServiceInfo, rslt, examineDetail);
			em.persist(labSeviceDetail);
			em.getTransaction().commit();
		} catch (Exception e) {
			rslt = e.getMessage();
		}
		return rslt;
	}

	public String update(int idLabServiceDetail, String result) {
		EntityManager em = this.getEm();
		String rslt = "success";
		try {
			em.getTransaction().begin();
			LabServiceDetail labSeviceDetail = this.readById(idLabServiceDetail);
			labSeviceDetail.setResult(result);
			em.merge(labSeviceDetail);
			em.getTransaction().commit();
		} catch (Exception e) {
			rslt = e.getMessage();
		}
		return rslt;
	}

}
